package org.wheel;

/**
 * JSON 扁平化
 */
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.wheel.plugins.toolkit.util.bean.DataHandlerUtil;
import org.wheel.plugins.toolkit.util.bean.DynaBean;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class JsonFlattener {

    public static JSONObject flattenJson(JSONObject json) throws JSONException {
        return flattenJson(json, "");
    }

    private static JSONObject flattenJson(JSONObject json, String prefix) throws JSONException {
        JSONObject flatJson = new JSONObject();

        for (Iterator it = json.keys(); it.hasNext(); ) {
            String key = (String) it.next();
            Object value = json.get(key);

            if (value instanceof JSONObject) {
                JSONObject nestedJson = flattenJson((JSONObject) value, prefix + key + ".");
                for (Iterator iter = nestedJson.keys(); iter.hasNext(); ) {
                    String nestedKey = (String) iter.next();
//                    flatJson.put(prefix + nestedKey, nestedJson.get(nestedKey).getClass().getSimpleName());
                    flatJson.put(prefix + nestedKey, nestedJson.get(nestedKey));
                }
            } else if (value instanceof JSONArray) {
                JSONArray array = (JSONArray) value;
//                for (int i = 0; i < array.length(); i++) {
                for (int i = 0; i < 1; i++) {
                    if (array.get(i) instanceof JSONObject) {
                        JSONObject arrayItem = flattenJson(array.getJSONObject(i), prefix + key + ".");
                        for (Iterator iter = arrayItem.keys(); iter.hasNext(); ) {
                            String itemKey = (String) iter.next();
//                            flatJson.put(itemKey, arrayItem.get(itemKey).getClass().getSimpleName());
                            flatJson.put(itemKey, arrayItem.get(itemKey));
                        }
                    } else {
//                        flatJson.put(prefix + key + "." + i, array.get(i).getClass().getSimpleName());
                        flatJson.put(prefix + key + "." + i, array.get(i));
                    }
                }
            } else {
                flatJson.put(prefix + key, value);
            }
        }

        return flatJson;
    }

    public static void main(String[] args) throws JSONException, JsonProcessingException {
        JSONArray array = new JSONArray();
        JSONObject company = new JSONObject();
        company.put("name", "alibaba");
        company.put("address", "hangzhou");

        JSONArray ttk = new JSONArray();
        ttk.put(1);
        ttk.put(2);
        ttk.put(3);
        ttk.put(4);
        ttk.put(5);
        array.put(company);

        JSONObject jsonData = new JSONObject()
                .put("person", new JSONObject()
                        .put("name", "zhangsan")
                        .put("age", 18))
                .put("company", array)
                .put("ttk", ttk);
        System.out.println(jsonData.toString());

        JSONObject flatData = flattenJson(jsonData);

        System.out.println(flatData.toString());

        System.out.println("========================= 反向输出 ========================= ");
        // 反向输出
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> map = objectMapper.readValue(flatData.toString(), Map.class);
        List<DynaBean> listDBean = DataHandlerUtil.getInstance().buildDynamicObject(List.of(map));
        System.out.println(listDBean);

        List<DynaBean> resultList = DataHandlerUtil.getInstance().buildDynamicObjectGroup(listDBean, null);
        resultList.forEach(System.out::println);
    }
}
